package com.examsys.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.examsys.po.AdminRolesSettings;
import com.examsys.service.AdminRolesSettingsService;
import com.examsys.util.JsonUtils;
import com.examsys.util.Page;
/**
 * 系统功能管理控制层
 *
 */
@WebServlet("/adminRolesSettings")
public class AdminRolesSettingsController extends BaseController{

	//log4j日志对象
	private Logger log=Logger.getLogger(this.getClass());
	
	private AdminRolesSettingsService adminRolesSettingsService;
	
	/**
	 * 初始化工作，这里主要是给业务逻辑层属性做初始化
	 */
	@Override
	public void init() throws ServletException {
		super.init();
		adminRolesSettingsService=lookUp("adminRolesSettingsService");
	}
	
	/**
	 * 保存添加
	 * @param adminRolesSettings
	 * @param parent_id
	 * @return
	 * @throws IOException 
	 */
	public void addSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String pid = request.getParameter("parent_id");
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String porder = request.getParameter("porder");
		String url = request.getParameter("url");
		Integer parent_id=null;
		
		if(pid!=null&&!"".equals(pid)){
			parent_id=Integer.valueOf(pid);
		}
		
		Integer porderi=null;
		
		if(porder!=null&&!"".equals(porder)){
			porderi=Integer.valueOf(porder);
		}
		
		AdminRolesSettings adminRolesSettings=new AdminRolesSettings();
		adminRolesSettings.setName(name);
		adminRolesSettings.setPorder(porderi);
		adminRolesSettings.setUrl(url);
		adminRolesSettings.setCode(code);
		
		log.info("接收到页面添加的数据："+adminRolesSettings+",父级编号："+parent_id);//把adminRolesSettings放到日志
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		try {
			if(parent_id!=null){
				AdminRolesSettings pRolesSettings=adminRolesSettingsService.get(parent_id);//获取父级编号，并用rolesSettings接收
				adminRolesSettings.setAdminRolesSettings(pRolesSettings);//把接收了父级编号的rolesSettings放到自己的adminRolesSettings里面去
			}
			boolean flag = adminRolesSettingsService.add(adminRolesSettings);//然后把adminRolesSettings添加进去
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
			
		} catch (Exception e) {
			log.error("添加失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
		
	}
	
	/**
	 * 初始化修改
	 * @param id 待修改的功能的编号
	 * @return
	 * @throws IOException 
	 */
	public  void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id=Integer.valueOf(request.getParameter("id"));
		log.info("开始初始化编号为"+id+"的系统功能数据供前端修改");//把id放到日志
		AdminRolesSettings rolesSettings=null;
		try {
			//通过id去获取系统功能信息
			rolesSettings=adminRolesSettingsService.get(id);//通过id拿到功能对象赋给rolesSettings变量
		} catch (Exception e) {
			log.error("初始化修改失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(rolesSettings));
		
	}
	
	/**
	 * 保存修改
	 * @param adminRolesSettings
	 * @param parent_id
	 * @return
	 * @throws IOException 
	 */
	public void updateSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String id = request.getParameter("id");
		String pid = request.getParameter("parent_id");
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String porder = request.getParameter("porder");
		String url = request.getParameter("url");
		Integer parent_id=null;
		
		if(pid!=null&&!"".equals(pid)){
			parent_id=Integer.valueOf(pid);
		}
		
		Integer porderi=null;
		
		if(porder!=null&&!"".equals(porder)){
			porderi=Integer.valueOf(porder);
		}
		
		Integer idd=null;
		
		if(id!=null&&!"".equals(id)){
			idd=Integer.valueOf(id);
		}
		
		log.info("接收到页面修改的数据："+idd+",父级编号："+parent_id);//把adminRolesSettings放到日志
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		try {
			
			AdminRolesSettings oldrolesSettings=adminRolesSettingsService.get(idd);//通过id找到对应的这一条系统功能信息
			//找到之后，把名字放进去给它
			oldrolesSettings.setName(name);
			oldrolesSettings.setUrl(url);
			oldrolesSettings.setPorder(porderi);
			oldrolesSettings.setCode(code);
			
			if(parent_id!=null){
				AdminRolesSettings pRolesSettings=adminRolesSettingsService.get(parent_id);//获取父级编号，并用rolesSettings接收
				oldrolesSettings.setAdminRolesSettings(pRolesSettings);//设置关联，把接收了的父级编号对应的pRolesSettings放到自己的adminRolesSettings属性里面去
			}else{
				oldrolesSettings.setAdminRolesSettings(null);
			}
			//调用修改的方法，修改原先的信息
			boolean flag = adminRolesSettingsService.update(oldrolesSettings);
			
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		
		} catch (Exception e) {
			log.error("修改失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 删除系统功能
	 * @param id待删除的功能的编号
	 * @return
	 * @throws IOException 
	 */
	public  void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id=Integer.valueOf(request.getParameter("id"));
		log.info("开始删除编号为"+id+"的系统功能");//把id放到日志
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		try {
			//通过id去删除数据
			boolean flag = adminRolesSettingsService.delete(id);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			log.error("删除失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 系统功能管理页面
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		log.info("进入系统功能管理页面");
		request.getRequestDispatcher("adminRolesSettings/AdminRolesSettingsManager.jsp").forward(request, response);
	}
	
	/**
	 * 系统功能父级下拉框数据
	 * @return
	 * @throws IOException 
	 */
	public  void parentDatas(HttpServletRequest request, HttpServletResponse response) throws IOException{
		log.info("获取系统功能父级下拉框数据");
		List<AdminRolesSettings> adminRolesrolesSettingsList=new ArrayList<AdminRolesSettings>();//待返回集合数据
		try {
			adminRolesrolesSettingsList = adminRolesSettingsService.getList();//获取所有系统功能
		} catch (Exception e) {
			log.error("获取系统功能父级下拉框数据失败",e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(adminRolesrolesSettingsList));
	}
	
	/**
	 * 系统功能管理表格数据
	 * @param adminRolesSettings 查询条件
	 * @param page 页码
	 * @param rows 每一页行数
	 * @return
	 * @throws IOException 
	 */
	public void selectDatas(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String pid = request.getParameter("parent_id");
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		
		Integer parent_id=null;
		
		if(pid!=null&&!"".equals(pid)){
			parent_id=Integer.valueOf(pid);
		}
		
		Integer ppage=null;
		
		if(page!=null&&!"".equals(page)){
			ppage=Integer.valueOf(page);
		}
		
		Integer rrows=null;
		
		if(rows!=null&&!"".equals(rows)){
			rrows=Integer.valueOf(rows);
		}
		
		AdminRolesSettings adminRolesSettings=new AdminRolesSettings();
		adminRolesSettings.setName(name);
		adminRolesSettings.setCode(code);
		
		log.info("开始获取系统功能管理表格数据");
		Map jsonDatas=new HashMap();//待返回json集合数据;
		try {
			if(parent_id!=null){
				AdminRolesSettings pRolesSettings=adminRolesSettingsService.get(parent_id);//获取父级编号，并用rolesSettings接收
				adminRolesSettings.setAdminRolesSettings(pRolesSettings);//设置关联，把接收了的父级编号对应的pRolesSettings放到自己的adminRolesSettings属性里面去
			}else{
				adminRolesSettings.setAdminRolesSettings(null);
			}
			
			//分页处理
			Page<AdminRolesSettings> pageObj = adminRolesSettingsService.getList(adminRolesSettings,ppage,rrows);
			//取记录总条数
			jsonDatas.put("total", pageObj.getTotal());//总数
			jsonDatas.put("rows", pageObj.getResultlist());//前端需要的行数据
			
		} catch (Exception e) {
			log.error("获取系统功能管理表格数据失败",e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
		
	}
	
	/**
	 * 系统功能添加页面
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		log.info("进入系统功能添加页面");
		request.getRequestDispatcher("adminRolesSettings/AdminRolesSettingsAdd.jsp").forward(request, response);
	}
}

